////////////////////////////////////////////////
// -- DepthHUD Lite                           //
// by Hurricaaane (Ha3)                       //
//                                            //
// http://www.youtube.com/user/Hurricaaane    //
//--------------------------------------------//
// Cloud Loader                               //
////////////////////////////////////////////////

if dhlite_cloud then pcall(function() dhlite_cloud.Unmount() end) end

dhlite_cloud = {}
dhlite_cloudloader_version = 1.0

local DHLITE_CloudReceiverTimeoutDelay = 5

local DHLITE_CloudReceiverQueried    = 0
local DHLITE_CloudReceiverResponded  = false
local DHLITE_CloudReceiverAborted    = false

local DHLITE_CloudReceiverNumTries   = 3

local DHLITE_CloudContents = ""
--local DHLITE_CloudComposedContents = ""
local DHLITE_CloudFileList = {}
local DHLITE_CloudSubContents = {}
local DHLITE_Origin = "http://depthhudlite.googlecode.com/svn/trunk/lua/"
local DHLITE_Start  = "cd_dhlite_includelist.lua"

function dhlite_cloud.BuildBase()
	pcall(function() if dhlite and dhlite.Unmount then dhlite.Unmount() end end)
	dhlite = {}
	dhlite_dat = {}
	
end

local function DHLITE_ReceiveCloud( contents , size )
	if DHLITE_CloudReceiverResponded or DHLITE_CloudReceiverAborted then return end
	
	//debug should perform checks here
	DHLITE_CloudContents = contents
	
	DHLITE_CloudReceiverResponded = true
	
	// debug direct load
	dhlite_cloud.Load()
	
end

function dhlite_cloud.Load()
	if DHLITE_CloudContents == "" then return end
	
	ADDON_PROP = {}
	
	local bOkay, strErr = pcall(function() dhlite_cloud.InternalLoad() end)
	local bCouldLoad = false
	
	if not bOkay then
		print(" > " .. DHLITE_NAME .. " Cloud Contents failed to pass semantics : ".. strErr)
		
	elseif (ADDON_PROP == nil) or (type(ADDON_PROP) ~= "table") or (#ADDON_PROP == 0) then
		print(" > " .. DHLITE_NAME .. " Cloud Contents misses standard table.")
		
	else
		DHLITE_CloudFileList = table.Copy(ADDON_PROP)
		bCouldLoad = true
		
		print(" > " .. DHLITE_NAME .. " Cloud Contents now gathering Cloud Contents...")
		dhlite_cloud.GatherSubContents()
		
	end

	ADDON_PROP = nil
	
	if not bCouldLoad then
		print(" > " .. DHLITE_NAME .. " couldn't load from Cloud. Now using Locale.")
		dhlite_cloud.LoadLocale()
		
	end
	
end

local function DHLITE_ReceiveSubContents( args ,contents , size )
	if DHLITE_CloudReceiverResponded or DHLITE_CloudReceiverAborted then return end
	
	local packet_num = args[1]
	DHLITE_CloudSubContents[packet_num] = contents
	print(" > " .. DHLITE_NAME .. " Cloud Contents received packet #".. packet_num .." of ".. #DHLITE_CloudFileList .. " :: ".. DHLITE_CloudFileList[packet_num])
	
	if table.Count(DHLITE_CloudSubContents) == #DHLITE_CloudFileList then
		DHLITE_CloudReceiverResponded = true
		print(" > " .. DHLITE_NAME .. " Cloud Contents trying to mount from Cloud...")
		dhlite_cloud.LoadComposition()
		
	end
	
end

function dhlite_cloud.GatherSubContents()
	for k,path in pairs( DHLITE_CloudFileList ) do
		http.Get( DHLITE_Origin ..  path , "", DHLITE_ReceiveSubContents, k )
		dhlite_cloud.CheckTimeout( true )
		
	end
	
end

function dhlite_cloud.InternalLoad()
	CompileString( DHLITE_CloudContents , "DHLITE_InternalLoad" )()
	
end

function dhlite_cloud.LoadComposition()
	
	local bOkay, strErr = pcall(function() dhlite_cloud.InternalCompose() end)
	local bCouldLoad = false
	
	if not bOkay then
		print(" > " .. DHLITE_NAME .. " Cloud Contents Composition failed to pass semantics : ".. strErr)
		
		print(" > " .. DHLITE_NAME .. " couldn't load from Cloud. Now using Locale.")
		dhlite_cloud.LoadLocale()
		
	else
		dhlite_cloud.AttemptMount()
		
	end
	
end

function dhlite_cloud.InternalCompose()
	dhlite_cloud.BuildBase()
	for i = 1, #DHLITE_CloudSubContents do
		CompileString( DHLITE_CloudSubContents[i] , "DHLITE_INTERNAMCOMPOSE__PACKET" .. tostring(i) )()
		
	end
	
end

function dhlite_cloud.AttemptMount()
	local bCouldLoad = false

	local strBivalErr = ""
	bCouldLoad, strBivalErr = pcall(function() dhlite.Mount() end)
	if not bCouldLoad then
		print(" > " .. DHLITE_NAME .. " Cloud Contents failed to mount : ".. strBivalErr)
		//Now used in buildbase
		//pcall(function() dhlite.Unmount() end)
		
		print(" > " .. DHLITE_NAME .. " couldn't load from Cloud. Now using Locale.")
		dhlite_cloud.LoadLocale()
	else
		print(" > " .. DHLITE_NAME .. " successfully loaded from Cloud.")
		
	end
	
end

function dhlite_cloud.CheckTimeout( bFirst )
	if bFirst then
		timer.Create("DHLITE_CLOUD_TIMEOUT", DHLITE_CloudReceiverTimeoutDelay, 1, dhlite_cloud.CheckTimeout)
		DHLITE_CloudReceiverQueried = 0
		DHLITE_CloudReceiverResponded = false
		DHLITE_CloudReceiverAborted   = false

	elseif not DHLITE_CloudReceiverResponded then
		DHLITE_CloudReceiverQueried = DHLITE_CloudReceiverQueried + 1
		
		if DHLITE_CloudReceiverQueried <= DHLITE_CloudReceiverNumTries then
			print(" > " .. DHLITE_NAME .. " Cloud Contents failed to respond on check #" .. DHLITE_CloudReceiverQueried .. ". Waiting.")
			timer.Create("DHLITE_CLOUD_TIMEOUT", DHLITE_CloudReceiverTimeoutDelay, 1, dhlite_cloud.CheckTimeout)
		
		else
			print(" > " .. DHLITE_NAME .. " Cloud Contents failed to respond on check #" .. DHLITE_CloudReceiverQueried .. ".")
			dhlite_cloud.Abort()
			
		end
		
	end
		
	
end

function dhlite_cloud.Abort()
	if DHLITE_CloudReceiverAborted then return end
	
	print(" > " .. DHLITE_NAME .. " Cloud Contents loading aborted. Now using Locale.")
	DHLITE_CloudReceiverAborted = true
	dhlite_cloud.LoadLocale()
	
end

function dhlite_cloud.LoadLocale()
	dhlite_cloud.BuildBase()
	
	ADDON_PROP = {}
	
	include( DHLITE_Start )
	
	for i = 1, #ADDON_PROP do
		include( ADDON_PROP[i] )
		
	end
	
	ADDON_PROP = nil
	
	if dhlite.Mount then
		dhlite.Mount()
		
	end
	
end

function dhlite_cloud.Ask()
	print(" > " .. DHLITE_NAME .. " now trying to reach Cloud...")
	
	DHLITE_CloudReceiverQueried   = 0
	DHLITE_CloudContents = ""
	DHLITE_CloudFileList = {}
	DHLITE_CloudSubContents = {}

	dhlite_cloud.Query()
	dhlite_cloud.CheckTimeout( true )
	
end

function dhlite_cloud.Query()
	http.Get( DHLITE_Origin .. DHLITE_Start, "", DHLITE_ReceiveCloud )
	
end

function dhlite_cloud.Mount()
	concommand.Add( "dhlite_cloud_ask", dhlite_cloud.Ask )
	
end


function dhlite_cloud.Unmount()
	concommand.Remove( "dhlite_cloud_ask" )

end

dhlite_cloud.Mount()
